import { Component, OnInit, ViewChild, OnDestroy } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import {SimpleTableButton, SimpleTableColumn, SimpleTableComponent} from '@delon/abc';
import { NzMessageService, NzModalService, NzTabChangeEvent, UploadFile, NzNotificationService } from 'ng-zorro-antd';
import { CommService } from 'app/service/comm/comm.service';
import { Constant, ServiceResult } from '@core/constant';
import { EmitterService } from 'app/service/emit.service';
import { Router } from '@angular/router';
import { maService } from '../../../service/ma/ma.service';
import { catchError } from 'rxjs/operators';
import { of } from 'rxjs';
import { TranslateService } from '@ngx-translate/core';
import { Timeout } from '@shared/descriptor/timeout';
import {spListService} from "../../../service/spList/spList.service";
import {poListService} from "../../../service/poList/poList.service";
import {fhListService} from "../../../service/fhList/fhList.service";
import {formatDate} from "@angular/common";




@Component({
  selector: 'fhList-list',
  templateUrl: './fhList-list.component.html',
  styleUrls: ['./fhList-list.component.less']
})
export class fhListComponent implements OnInit, OnDestroy {

  // region 公共字段

  visible = false;
  // pageSize
  ps = 10;
  // 搜索表单
  q: any = {
    matnr:'',
    lifnr:"",
    name1: '',
    ebeln: '',
    vbeln1:'',
    dateRange: null,
    startDate:'',
    endDate:''
  };

  loading = false;
  // 表格组件
  @ViewChild('st') st: SimpleTableComponent;
  // 表格数据url
  url = this.fhListService.list();
  // region end

  // 搜索区域显示/隐藏标记
  // 显示图片
  previewVisible = false;
  // 大图地址
  previewImage = '';
  imgSuffix = '';

  years = [];
  userGroups = [];
  brands = [];
  seasons = [];
  checkedList = [];
  data = [];
  uploadExcelUrl = this.spListService.uploadfh();



  columns: SimpleTableColumn[] = [
    { title: '', index: 'key', type: 'checkbox' },
    {
      title: '操作',
      buttons: [

        {
          text: '打印标签',
          type: 'link',
          click: (record: any) => this.packattbone(record.id),
        }
        ,
        {
          text: '修改',
          type: 'link',
          click: (record: any) => this.router.navigate(['/fhList/fhList/edit/'+record.id]),
          iif: (item: any, btn: SimpleTableButton, column: SimpleTableColumn) => {
            return item.bpmStatus === '5'
          }
        },
        {
          text: '检验详情',
          type: 'link',
          click: (record: any) => this.router.navigate(['/fhList/fhList/jyxq/'+record.id]),
          iif: (item: any, btn: SimpleTableButton, column: SimpleTableColumn) => {
            return item.qmMenge6 === '5'
          }
        }
      ]
    }   ,
    // { title: '发货单号', index: 'vbeln' },
    // { title: '发货项目', index: 'posnr' },
    { title: '103凭证', index: 'mblnr' },
    { title: '104凭证', index: 'qmRemark4' },

    { title: '质检员', index: 'qmRemark5' },

    { title: '105凭证', index: 'qmRemark6' },
    { title: '状态', index: 'vstatus' },
    { title: '在途天数', index: 'ztdatets' },

    { title: '快递单号', index: 'vbeln1' },
    { title: '供应商', index: 'lifnr' },
    { title: '供应商名称', index: 'name1' },
    { title: '发货时间', index: 'erdat' },
    { title: '采购订单', index: 'ebenl' },
    { title: '采购订单行项目', index: 'ebelp' },
    { title: '物料编码', index: 'matnr' },
    { title: '物料描述', index: 'maktx' },
    { title: '供应商物料编码', index: 'lifmatnr' },
    { title: '供应商物料描述', index: 'lifmaktx' },

    { title: '采购订单文本备注', index: 'ptext' },

    { title: '箱号', index: 'xianghao' },
    { title: '重量', index: 'weight' },

    { title: '单位', index: 'meins' },
    { title: '数量', index: 'menge' },
    { title: '备注', index: 'remark' },
    // { title: '退货数量', index: 'menge2' },

    // { title: '行项目', index: 'zeile' },
    // { title: '年度', index: 'mjahr' },

    // { title: '实收数', index: 'menge1' },
    { title: '良品数量', index: 'menge2' },
    { title: '不良数量', index: 'menge3' },
    { title: '抽检数量', index: 'qmMenge1' },
    { title: '不良比例', index: 'qmMenge2' },
    { title: '主要不良', index: 'qmRemark1' },
    { title: '质检备注', index: 'qmRemark2' },
    { title: '检验类型', index: 'qmPanding1' }
  ];

  constructor(
    private trans: TranslateService,
    public http: _HttpClient,
    public msg: NzMessageService,
    private modalSrv: ModalHelper,
    private comm: CommService,
    private cnt: Constant,
    private maService: maService,
    private emit: EmitterService,
    private router: Router,
    private modalService: NzModalService,
    private fhListService: fhListService,
    private notice: NzNotificationService,
    private spListService: spListService,
    private confirmServ: NzModalService,

  ) {}
  syncing = false;
  lastDate = null;

  ngOnDestroy() {
    if (this.inter) {
      clearInterval(this.inter);
    }
  }
  inter
  ngOnInit() {
    this.imgSuffix = this.comm.getGoodsUrl();
    this.emit.getMessage().subscribe(data => {
      if (data.text === 'RELOAD_GOODS_LIST') {
        this.st.load(1, this.q);
      }
    })
  }
  fullChange(val: boolean) {
    // this.scroll = val ? { y: '350px' } : { y: '230px' };
  }
  loaddata(wqtype){
    // console.log(wqtype);
    // this.q.bpmStatus=wqtype;
    this.st.load(1,this.q)
  }
  uploadProd(info: { file: UploadFile }) {
    if (info.file.status === 'uploading') {
      this.loading = true;
    }
    if (info.file.status === 'done') {
      this.loading = false;
      console.log("info1*********"+JSON.stringify(info.file.response[0]));
      this.st.load();
      if(info.file.response[0].piccontent==='success'){
        this.notice.create('success', '成功', info.file.response[0].piccontent);
      }else{
        this.notice.create('error', '失败', info.file.response[0].piccontent);
      }
    }
    if (info.file.status === 'error') {
      this.loading = false;
      this.notice.create('error', '失败', '导入失败');
    }
  }

  getall(){
    if(!this.q.lifnr){
      this.msg.warning("供应商编号");
      return;
    }
    this.spListService.getekbe(this.q.lifnr).pipe(
      catchError(err => { return of(err); })
    ).subscribe(ret=>{
      if (ret.ok) {

        this.st.reload();
      } else {
        this.msg.error(ret.message);
      }
    });


  }
  checkboxChange(list: any[]) {
    this.checkedList = list;
  }
  close(): void {
    this.visible = false;
  }
  cancel() {
    this.repdate = null;
  }
  repdate = null;

  downurl='';

  packatta(){
    this.loading = true;
    let conmapList = [];
    let  conhead={
      orderNo:"no",
      conNo:this.q.vbeln1,
      conmapList:JSON.parse(JSON.stringify(conmapList))
    }
    this.fhListService.batchdown(conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;
        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })


  }

  packattb(id){

    this.loading = true;

     let conmapList = [];


    let  conhead={
      orderNo:"no",
      conNo:"",
      conBy1:this.q.vbeln1,
      conmapList:JSON.parse(JSON.stringify(conmapList))
    }
    this.fhListService.batchdownbq(conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })

  }
  packattbone(id){
    this.loading = true;

    let conmapList = [];


    let  conhead={
      orderNo:id,
      conNo:id,
      conBy1:"",
      conmapList:JSON.parse(JSON.stringify(conmapList))
    }
    this.fhListService.batchdownbq(conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })

  }
  opennewwindow(url){

    const newWindow = window.open(url, '_blank');
    newWindow.document.title = '下载';

  }



  showdownfirst(){
    if (this.checkedList.length === 0) {
      this.msg.warning('请选择');
      return;
    }
    this.confirmServ.confirm({
      nzTitle: '确定批量导出发货?',
      nzOnOk: () => {
        this.showdown();
      },
    });
  }

  showdownbqfirst(){
    if (this.checkedList.length === 0) {
      this.msg.warning('请选择');
      return;
    }
    this.confirmServ.confirm({
      nzTitle: '确定批量导出标签?',
      nzOnOk: () => {
        this.showdownbq();
      },
    });
  }

  showdownqmfirst(){
    if (this.checkedList.length === 0) {
      this.msg.warning('请选择');
      return;
    }
    this.confirmServ.confirm({
      nzTitle: '确定批量导出质检报告?',
      nzOnOk: () => {
        this.showdownbq();
      },
    });
  }
  showdown() {

    this.loading = true;


    const ids = this.checkedList.map(v => v.id);
    let conmapList = [];


    let  conhead={
      orderNo:ids.toString(),
      conmapList:JSON.parse(JSON.stringify(conmapList))
    }
    this.fhListService.batchdown(conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })

  }

  down(){

  }


  showdownbq() {
    this.loading = true;
    const ids = this.checkedList.map(v => v.id);
    let conmapList = [];
    let  conhead={
      orderNo:ids.toString(),
      conmapList:JSON.parse(JSON.stringify(conmapList))
    }
    this.fhListService.batchdownbq(conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;
        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }

  delbyid(id) {
    this.fhListService.delPro(id).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(ret=>{
      if (ret.ok === true) {
        this.st.reload();
      } else {
        this.msg.error(ret.message);
      }
    });


  }

  pacafirst(){
    if(this.q.vbeln1 == '' || this.q.vbeln1 == null){
      this.msg.warning("快递单");
      return;
    }
    this.confirmServ.confirm({
      nzTitle: '确定导出发货?',
      nzOnOk: () => {
        this.packatta();
      },
    });
  }
  pacbfirst(){
    if(this.q.vbeln1 == '' || this.q.vbeln1 == null){
      this.msg.warning("快递单");
      return;
    }
    this.confirmServ.confirm({
      nzTitle: '确定打印物料标签?',
      nzOnOk: () => {
        this.packattb('');
      },
    });
  }
  delvbelnfirst(){
    if(this.q.vbeln1 == '' || this.q.vbeln1 == null){
      this.msg.warning("快递单");
      return;
    }
    this.confirmServ.confirm({
      nzTitle: '确定取消发货?',
      nzOnOk: () => {
        this.delvbeln1();
      },
    });
}


  // vbeln103first(){
  //   if(this.q.vbeln1 == '' || this.q.vbeln1 == null){
  //     this.msg.warning("快递单");
  //     return;
  //   }
  //   this.confirmServ.confirm({
  //     nzTitle: '确定取消发货?',
  //     nzOnOk: () => {
  //       this.delvbeln1();
  //     },
  //   });
  // }
  delvbeln1(){
    this.fhListService.delprodetail(this.q.vbeln1).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      if(data.ok){
        this.msg.success("取消成功");
        this.st.reload();
      }else{
        this.msg.success(data.msg);
      }
    })
  }

  dateChange(result: Date[]): void {
    if (result.length !== 0) {
      this.q.startDate = formatDate(result[0], 'yyyy-MM-dd', 'en-US');
      this.q.endDate = formatDate(result[1], 'yyyy-MM-dd', 'en-US');
    } else {
      this.q.startDate = '';
      this.q.endDate = '';
    }
  }
  packattaall(){
    this.loading = true;

    let conmapList = [];

    try{
      let dto1 ={
        conKey:"ebeln",
        conValue:this.q.ebeln,
      }
      conmapList.push(dto1);

      let dto2 ={
        conKey:"matnr",
        conValue:this.q.matnr,
      }
      conmapList.push(dto2);

      let dto3 ={
        conKey:"lifnr",
        conValue:this.q.lifnr,
      }

      conmapList.push(dto3);

      let dto4 ={
        conKey:"name1",
        conValue:this.q.name1,
      }

      conmapList.push(dto4);

      let dto5 ={
        conKey:"vbeln1",
        conValue:this.q.vbeln1,
      }

      conmapList.push(dto5);

      let dto6 ={
        conKey:"startDate",
        conValue:this.q.startDate,
      }

      conmapList.push(dto6);
      let dto7 ={
        conKey:"endDate",
        conValue:this.q.endDate,
      }

      conmapList.push(dto7);


    }catch (e) {

    }
    let  conhead={
      contempPath:"https://zhaodui.oss-cn-shenzhen.aliyuncs.com/doc/htmb/hxhtmb.docx",
      conmapList:JSON.parse(JSON.stringify(conmapList)),
    }
    this.comm.genfhAll(this.q.ebeln,conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }


  packattaqmall(){
    this.loading = true;

    let conmapList = [];

    try{
      let dto1 ={
        conKey:"ebeln",
        conValue:this.q.ebeln,
      }
      conmapList.push(dto1);

      let dto2 ={
        conKey:"matnr",
        conValue:this.q.matnr,
      }
      conmapList.push(dto2);

      let dto3 ={
        conKey:"lifnr",
        conValue:this.q.lifnr,
      }

      conmapList.push(dto3);

      let dto4 ={
        conKey:"name1",
        conValue:this.q.name1,
      }

      conmapList.push(dto4);

      let dto5 ={
        conKey:"vbeln1",
        conValue:this.q.vbeln1,
      }

      conmapList.push(dto5);

      let dto6 ={
        conKey:"startDate",
        conValue:this.q.startDate,
      }

      conmapList.push(dto6);
      let dto7 ={
        conKey:"endDate",
        conValue:this.q.endDate,
      }

      conmapList.push(dto7);


    }catch (e) {

    }
    let  conhead={
      contempPath:"https://zhaodui.oss-cn-shenzhen.aliyuncs.com/doc/htmb/hxhtmb.docx",
      conmapList:JSON.parse(JSON.stringify(conmapList)),
    }
    this.comm.genfhqmAll(this.q.ebeln,conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }

}
